<template>
  <div>
    <h2 class="page-title">
      <el-icon><VideoCamera /></el-icon>
      视频管理
    </h2>
    <div class="card">
      <div class="table-operations">
        <el-button
          type="primary"
          :icon="Plus"
          @click="handleUploadVideo"
          v-if="!$store.getters.isAdmin"
        >
          上传视频
        </el-button>
        <el-input
          placeholder="搜索视频"
          style="width: 300px"
          v-model="videoSearch"
          clearable
        >
          <template #append>
            <el-button :icon="Search"></el-button>
          </template>
        </el-input>
      </div>

      <el-table
        :data="filteredVideoList"
        stripe
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column prop="title" label="视频标题"></el-table-column>
        <el-table-column
          prop="pond"
          label="所属虾塘"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="uploader"
          label="上传者"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="uploadTime"
          label="上传时间"
          width="120"
        ></el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag
              v-if="scope.row.status === 'approved'"
              type="success"
              effect="dark"
            >
              <el-icon><SuccessFilled /></el-icon>
              已审核
            </el-tag>
            <el-tag
              v-else-if="scope.row.status === 'rejected'"
              type="danger"
              effect="dark"
            >
              <el-icon><CloseBold /></el-icon>
              已拒绝
            </el-tag>
            <el-tag v-else type="warning" effect="dark">
              <el-icon><Clock /></el-icon>
              待审核
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template #default="scope">
            <el-button size="small" :icon="View" @click="handleView(scope.row)"
              >查看</el-button
            >
            <el-button
              size="small"
              type="primary"
              :icon="Edit"
              @click="handleReview(scope.row)"
              v-if="$store.getters.isAdmin && scope.row.status === 'pending'"
            >
              审核
            </el-button>
            <el-button
              size="small"
              type="danger"
              :icon="Delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import {
  VideoCamera,
  SuccessFilled,
  CloseBold,
  Clock,
} from "@element-plus/icons-vue";

export default {
  name: "VideoList",
  setup() {
    const Plus = ElementPlusIconsVue.Plus;
    const Search = ElementPlusIconsVue.Search;
    const View = ElementPlusIconsVue.View;
    const Edit = ElementPlusIconsVue.Edit;
    const Delete = ElementPlusIconsVue.Delete;

    const videoSearch = ref("");
    const loading = ref(false);
    const currentPage = ref(1);
    const pageSize = ref(10);
    const total = ref(0);

    const videoList = ref([
      {
        id: 1,
        title: "1号虾塘日常监测",
        pond: "1号虾塘",
        uploader: "李养殖户",
        uploadTime: "2023-06-15",
        status: "approved",
      },
      {
        id: 2,
        title: "3号虾塘异常情况",
        pond: "3号虾塘",
        uploader: "王养殖户",
        uploadTime: "2023-06-15",
        status: "pending",
      },
      {
        id: 3,
        title: "投饵过程记录",
        pond: "2号虾塘",
        uploader: "李养殖户",
        uploadTime: "2023-06-14",
        status: "approved",
      },
      {
        id: 4,
        title: "5号虾塘水质监测",
        pond: "5号虾塘",
        uploader: "赵养殖户",
        uploadTime: "2023-06-14",
        status: "rejected",
      },
      {
        id: 5,
        title: "夜间虾塘活动",
        pond: "4号虾塘",
        uploader: "赵养殖户",
        uploadTime: "2023-06-13",
        status: "approved",
      },
    ]);

    const filteredVideoList = computed(() => {
      if (!videoSearch.value) {
        return videoList.value;
      }
      return videoList.value.filter(
        (video) =>
          video.title.includes(videoSearch.value) ||
          video.pond.includes(videoSearch.value) ||
          video.uploader.includes(videoSearch.value)
      );
    });

    const handleUploadVideo = () => {
      ElMessage.info("上传视频功能");
    };

    const handleView = (row) => {
      ElMessage.info(`查看视频: ${row.title}`);
    };

    const handleReview = (row) => {
      ElMessage.info(`审核视频: ${row.title}`);
    };

    const handleDelete = (row) => {
      ElMessageBox.confirm(`确定要删除视频 "${row.title}" 吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          videoList.value = videoList.value.filter(
            (video) => video.id !== row.id
          );
          ElMessage.success("删除成功");
        })
        .catch(() => {});
    };

    const handleSizeChange = (val) => {
      pageSize.value = val;
      currentPage.value = 1;
    };

    const handleCurrentChange = (val) => {
      currentPage.value = val;
    };

    onMounted(() => {
      total.value = videoList.value.length;
    });

    return {
      VideoCamera,
      SuccessFilled,
      CloseBold,
      Clock,
      Plus,
      Search,
      View,
      Edit,
      Delete,
      videoSearch,
      loading,
      videoList,
      filteredVideoList,
      currentPage,
      pageSize,
      total,
      handleUploadVideo,
      handleView,
      handleReview,
      handleDelete,
      handleSizeChange,
      handleCurrentChange,
    };
  },
};
</script>

<style scoped>
.page-title {
  font-size: 20px;
  margin-bottom: 20px;
  color: #1b9a7c;
  padding-bottom: 10px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-title .el-icon {
  font-size: 24px;
}

.table-operations {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}

.card {
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}
</style>
